import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'

import axios from 'axios'

const user = {
  state: {
    token: getToken(),
    id: '',
    name: '',
    nickName: '',
    avatar: '',
    position: '',
    roles: [],
    permissions: [],
    applicationList: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_NICK_NAME: (state, nickName) => {
      state.nickName = nickName
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_POSITION: (state, position) => {
      state.position = position
    },
    SET_APPLICATION_LIST: (state, applicationList) => {
      state.applicationList = applicationList
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      const password = userInfo.password
      const code = userInfo.code
      const uuid = userInfo.uuid
      return new Promise((resolve, reject) => {

        axios({
          url: `${process.env.VUE_APP_LOGIN_API}/login`,
          method: 'post',
          headers: {
            isToken: false,
            repeatSubmit: false
          },
          data: {
            account: username,
            pwd: password,
            captcha: code,
            captchaId: uuid
          }
        })
          .then(res => {
            res = res.data;
            if (res.rtn == '1') {

              setToken(res.result.token)
              commit('SET_TOKEN', res.result.token)

              const user = res.result.userInfo
              // commit('SET_ROLES', ['ROLE_DEFAULT'])
              // commit('SET_PERMISSIONS', ['*:*:*'])
              // commit('SET_POSITION', user.position)
              commit('SET_ID', user.userId)
              commit('SET_NAME', user.orgName + " | "+ user.positionName)
              commit('SET_NICK_NAME', user.roleName)
              // commit('SET_AVATAR', defAva)
              resolve()
            }else{
              reject(res.msg)
            }
          })
          .catch(error => {
            reject(error)
          });
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo({ userToken: getToken() }).then(res => {
          let per = (res.menuList || []).concat(res.functionList || []);
          per.push("remarks");
          commit('SET_PERMISSIONS', per)
          commit('SET_ROLES', ['ROLE_DEFAULT'])
          resolve(true)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          commit('SET_APPLICATION_LIST', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
